package com.photo.photePro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.photo.photePro.entity.Collection;
import com.photo.photePro.entity.Photo;
import com.photo.photePro.mapper.CollectionMapper;
import com.photo.photePro.service.CollectionService;
import com.photo.photePro.service.PhotoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 收藏服务实现类
 */
@Slf4j
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    private PhotoService photoService;

    @Override
    public Page<Collection> pageUserCollections(int page, int pageSize, Long userId) {
        Page<Collection> pageInfo = new Page<>(page, pageSize);
        
        // 1. 先查询用户收藏记录
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collection::getUserId, userId);
        queryWrapper.orderByDesc(Collection::getCreateTime);
        
        page(pageInfo, queryWrapper);
        
        // 2. 补充每个收藏记录的作品信息
        List<Collection> records = pageInfo.getRecords();
        if (records != null && !records.isEmpty()) {
            List<Long> photoIds = records.stream()
                    .map(Collection::getPhotoId)
                    .collect(Collectors.toList());
            
            // 批量查询作品信息
            List<Photo> photos = photoService.listByIds(photoIds);
            
            // 组装数据
            for (Collection collection : records) {
                for (Photo photo : photos) {
                    if (collection.getPhotoId().equals(photo.getId())) {
                        collection.setTitle(photo.getTitle());
                        collection.setDescription(photo.getDescription());
                        collection.setImgUrl(photo.getImgUrl());
                        collection.setAuthor(photo.getAuthor());
                        collection.setViewCount(photo.getViewCount());
                        break;
                    }
                }
            }
        }
        
        return pageInfo;
    }

    @Override
    public boolean isCollected(Long photoId, Long userId) {
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collection::getPhotoId, photoId);
        queryWrapper.eq(Collection::getUserId, userId);
        
        return count(queryWrapper) > 0;
    }

    @Override
    public boolean addCollection(Long photoId, Long userId) {
        // 先检查是否已收藏
        if (isCollected(photoId, userId)) {
            return true;
        }
        
        // 添加收藏
        Collection collection = new Collection();
        collection.setPhotoId(photoId);
        collection.setUserId(userId);
        collection.setCreateTime(LocalDateTime.now());
        
        return save(collection);
    }

    @Override
    public boolean removeCollection(Long photoId, Long userId) {
        LambdaQueryWrapper<Collection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collection::getPhotoId, photoId);
        queryWrapper.eq(Collection::getUserId, userId);
        
        return remove(queryWrapper);
    }
} 